/*
 * @brief :   
 * @date :  2021-11-xx
 * @version : v1.0.0
 * @copyright(c) 2020 : OptoMedic company Co.,Ltd. All rights reserved
 * @Change Logs:   
 * @date         author         notes:  
 */
#include "wdg.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <linux/watchdog.h>


/*
 * @brief : 打开看门狗  
 */
int wdg_open(const char *devname)
{
    return open(devname, O_RDWR);
}

/*
 * @brief : 开启or停止看门狗
 */
int wdg_on_off(int fd, wdg_on_off_t on_off)
{
    int op = WDIOS_ENABLECARD;

    if (on_off == WDG_OFF)
        op = WDIOS_DISABLECARD;

    ioctl(fd, WDIOC_SETOPTIONS, &op);
}

/*
 * @brief : 设置看门狗超时时间
 * @param : timeout 超时时间，单位s(秒)
 */
int wdg_set_timeout(int fd, int timeout)
{
    return ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
}

/*
 * @brief : 喂狗
 */
int wdg_feed(int fd)
{
    return ioctl(fd, WDIOC_KEEPALIVE, NULL);
}